/* 
 * ***** BEGIN LICENSE BLOCK ***** Version: NPL 1.1/GPL 2.0/LGPL 2.1 The
 * contents of this file are subject to the Netscape Public License Version 1.1
 * (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at http://www.mozilla.org/NPL/
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License. The
 * Original Code is lutsko.com code. The Initial Developer of the Original Code
 * is James Lutsko. Portions created by the Initial Developer are Copyright (C)
 * 2003 the Initial Developer. All Rights Reserved. Contributor(s):
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or the
 * GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which
 * case the provisions of the GPL or the LGPL are applicable instead of those
 * above. If you wish to allow use of your version of this file only under the
 * terms of either the GPL or the LGPL, and not to allow others to use your
 * version of this file under the terms of the NPL, indicate your decision by
 * deleting the provisions above and replace them with the notice and other
 * provisions required by the GPL or the LGPL. If you do not delete the
 * provisions above, a recipient may use your version of this file under the
 * terms of any one of the NPL, the GPL or the LGPL. ***** END LICENSE BLOCK
 * *****
 */

package autoteam.tables;

import java.util.List;

import javax.swing.JComboBox;
import javax.swing.table.AbstractTableModel;

import autoteam.Child;
import autoteam.League;
import autoteam.Nights;
import autoteam.Team;
import db.ColumnData;
import db.DBRecord;
import db.DataAccessObject;

public class TeamTableModel extends AutoTeamTableModel
{
	private static final long serialVersionUID = -5476391631484340498L;
	League currentLeague;
	JComboBox theBox;
	JComboBox coachBox;
	JComboBox coachKidBox;
	JComboBox nightBox;
	AbstractTableModel allocateModel;
	
	private DataAccessObject dao;

	private List<DBRecord> teamRecords;

	String[] columnNames =
	{ "Team", "Coach", "Coach's kid", "Practice Night", "Start Time",
			"Location" };

	public TeamTableModel(League newLeague, JComboBox box,
			AbstractTableModel _allocate)
	{
		currentLeague = newLeague;
		theBox = box;
		allocateModel = _allocate;
		dao = new DataAccessObject();

		/*
		 * DataAccessObject dao = new DataAccessObject("rsdb"); teamRecords =
		 * dao.getAllRecords("teams"); for (int i = 0; i < teamRecords.size();
		 * i++) { DBRecord curRecord = teamRecords.get(i); add(i); }
		 */
	}

	public void add(String teamName)
	{
		currentLeague.addTeam(teamName);
		fireTableDataChanged();
	}
	
	public void add(int j)
	{
		currentLeague.addTeam();
		fireTableDataChanged();
	}

	public void delete(int t)
	{
		currentLeague.deleteTeam(t);
		fireTableDataChanged();
	}

	public int getColumnCount()
	{
		return 6;
	}

	public int getRowCount()
	{
		return currentLeague.getNumberOfTeams();
	}

	public String getColumnName(int col)
	{
		return columnNames[col];
	}

	public Object getValueAt(int row, int col)
	{
		Team t = currentLeague.getTeam(row);
		if (t == null)
			return null;
		switch(col)
		{
		case 0:
			return t.name;
		case 1:
			return t.coach;
		case 2:
			return t.coachKid;
		case 3:
			return Nights.get(t.night);
		case 4:
			return t.startTime;
		case 5:
			return t.location;
		}
		return null;
	}

	/*
	 * JTable uses this method to determine the default renderer/ editor for
	 * each cell. If we didn't implement this method, then the last column would
	 * contain text ("true"/"false"), rather than a check box.
	 */
	public Class getColumnClass(int col)
	{
		return columnNames[0].getClass();
	}

	/*
	 * Don't need to implement this method unless your table's editable.
	 */
	public boolean isCellEditable(int row, int col)
	{
		return true;
	}

	public void setValueAt(Object value, int row, int col)
	{
		System.out.println("Setting team value at: (" +row+ ","+col+")");
		Team t = currentLeague.getTeam(row);
		if (t == null)
			return;
		switch(col)
		{
		case 0:
			t.name = (String) value;
			ColumnData[] identifiers = { new ColumnData("teamId",t.id) };
			ColumnData[] changes = { new ColumnData("teamName",t.name) };
			dao.updateRecord("teams", identifiers, changes);
			break;
		case 1:
			//TODO: tie coach in w/ database coach

			System.out.println((String) value);
			
			t.coach = (String) value;
			DBRecord teamRec = dao.getRecord("teams", new ColumnData("teamName",t.name));
			ColumnData[] cidentifiers = { new ColumnData("teamId",t.id) };
			ColumnData[] cchanges = { new ColumnData("coach",teamRec.getFieldValue("coach")) };
			dao.updateRecord("teams", cidentifiers, cchanges);
			break;
		case 2:
			t.coachKid = (Child) value;
			ColumnData[] ckidentifiers = { new ColumnData("teamId",t.id) };
			ColumnData[] ckchanges = { new ColumnData("coachKid",t.coachKid) };
			dao.updateRecord("teams", ckidentifiers, ckchanges);
			break;
		case 3:
			t.night = nightBox.getSelectedIndex();
			ColumnData[] nidentifiers = { new ColumnData("teamId",t.id) };
			ColumnData[] nchanges = { new ColumnData("practiceNight",t.night) };
			dao.updateRecord("teams", nidentifiers, nchanges);
			break;
		case 4:
			t.startTime = (String) value;
			ColumnData[] sidentifiers = { new ColumnData("teamId",t.id) };
			ColumnData[] schanges = { new ColumnData("startTime",t.startTime) };
			dao.updateRecord("teams", sidentifiers, schanges);
			break;
		case 5:
			t.location = (String) value;
			ColumnData[] lidentifiers = { new ColumnData("teamId",t.id) };
			ColumnData[] lchanges = { new ColumnData("location",t.location) };
			dao.updateRecord("teams", lidentifiers, lchanges);
			break;
		}
		currentLeague.setDirty();
	}
}
